package cn.zwxky.android.user.My;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sobot.chat.SobotApi;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.config.Config;
import com.zgscwjm.lsfbbasetemplate.utils.SN;
import com.zgscwjm.lsfbbasetemplate.utils.SPUtils;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import cn.zwxky.android.user.Login.LoginActivity;
import cn.zwxky.android.user.R;
import cn.zwxky.android.user.Utils.DataCleanManager;
import cn.zwxky.android.user.Utils.LittleUtils;
import cn.zwxky.android.user.Utils.PopWindowManager;
import cn.zwxky.android.user.Utils.URLString;
import cn.zwxky.android.user.Utils.WebActivity;


/**
 * 设置页面
 * Created by Administrator on 2017/3/15 0015.
 */
@ContentView(R.layout.aty_setting)
public class SettingActivity extends LsfbActivity {

    /**
     * 缓存
     */
    @ViewInject(R.id.aty_setting_text_number)
    private TextView text_number;

    /**
     * PopWindow管理类
     */
    private PopWindowManager replay;

    /**
     * 用于更新主线程UI
     */
    private Handler handler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
//            getCache();
            SN.SnackShow(text_number, "清理缓存完成");
            text_number.setText("0 KB");
        }
    };


    @Override
    public void initView() {
        LittleUtils.setsimpletitlebar(this, "设置");
    }

    @Override
    public void initEvent() {

    }

    /**
     * 点击事件
     * @param view
     */
    @OnClick({R.id.aty_setting_clean,R.id.aty_setting_about,R.id.aty_setting_help,R.id.aty_setting_feedbank,R.id.aty_setting_exit})
    public void onClick(View view){
        switch (view.getId()){
            //清除缓存
            case R.id.aty_setting_clean:
                    DataCleanManager.cleanApplicationData(getApplicationContext(), "");
                    handler.sendEmptyMessageDelayed(1000, 2000);
                    T.showShort(this, "缓存清理中...");

                break;
            //关于软件
            case R.id.aty_setting_about:
                startActivity(new Intent(this, WebActivity.class).putExtra("url", URLString.ABOUT).putExtra("title","关于软件"));
                break;
            //帮助
            case R.id.aty_setting_help:
                startActivity(new Intent(this, WebActivity.class).putExtra("url", URLString.HELP).putExtra("title","帮助"));
                break;
            //意见反馈
            case R.id.aty_setting_feedbank:
                startActivity(new Intent(this,FeedBackActivity.class));
                break;
            //退出
            case R.id.aty_setting_exit:
                replay = new PopWindowManager(SettingActivity.this, R.layout.pop_exit) {
                    @Override
                    public void onViewLister(View contentView, final PopWindowManager manager) {

                        final TextView pop_exit_yes = (TextView) contentView.findViewById(R.id.pop_exit_yes);
                        final TextView pop_exit_no = (TextView) contentView.findViewById(R.id.pop_exit_no);
                        final RelativeLayout RelativeLayout = (RelativeLayout) contentView.findViewById(R.id.RelativeLayout);

                        //退出
                        pop_exit_yes.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                LittleUtils.exit(SettingActivity.this);
                                SobotApi.exitSobotChat(SettingActivity.this);
                                startActivity(new Intent(SettingActivity.this, LoginActivity.class));
                            }
                        });

                        pop_exit_no.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                manager.hideWindow();
                            }
                        });

                        RelativeLayout.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                manager.hideWindow();
                            }
                        });

                    }
                };
                replay.showWindowsCenter(SettingActivity.this.getWindow().getDecorView());
                break;
        }
    }

    @Override
    public void initData() {
        getCache();
    }


    /**
     * 获取缓存数据
     */
    public void getCache() {
        try {
            text_number.setText(DataCleanManager.getTotalCacheSize(this.getApplicationContext()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
